package diaosidadui.homework;

import java.util.Random;

public class Test13 {
    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5};
        int[] arrRan = ranArr(arr);
        for (int i = 0; i < ranArr(arr).length; i++) {
            System.out.println(arrRan[i]);
        }
    }

    //生成一个元素随机且不重复的自然数数组
    public static int[]  ranArr(int[] arr) {
        int[] num = new int[5];
        Random r = new Random();

        for (int j = 0; j < arr.length; j++) {
            int ran = r.nextInt(arr.length) + 1;
            int index = -1;
            for (int i = 0; i <= j; i++) {
                //arr[i]
                if (num[i] == ran) {
                    index = 0;
                    break;
                }
            }
            if (index ==-1) {
                num[j] = ran;
            }else{
                j--;
            }
        }
        return num;


    }


}
